#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <windows.h>

int main(int argc, char *argv[])
{
    HANDLE handle;

    // write testing
    if (0) {
        // open the file
        handle = CreateFile("abc.txt",
                            GENERIC_READ | GENERIC_WRITE,
                            FILE_SHARE_READ | FILE_SHARE_WRITE,
                            NULL,
                            CREATE_NEW,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);

        if (handle == INVALID_HANDLE_VALUE) {
            fprintf(stderr, "create file failed: %d\n", GetLastError());
            return 1;
        }

        const char *str = "hello, kitty\n";
        int len = strlen(str);
        DWORD wbytes;

        // write content to file.
        if (WriteFile(handle,
                      str, len, &wbytes, NULL)) {
            printf("write succ, rbytes = %d\n", wbytes);
        }
        else {
            fprintf(stderr, "write file failed %d\n", GetLastError());
            return 1;
        }
    }

    // read testing
    if (0) {
        handle = CreateFile("abc.txt",
                            GENERIC_READ | GENERIC_WRITE,
                            FILE_SHARE_READ | FILE_SHARE_WRITE,
                            NULL,
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);

        if (handle == INVALID_HANDLE_VALUE) {
            fprintf(stderr, "open file failed: %d\n", GetLastError());
            return 1;
        }

        char buf[100];
        DWORD rbytes;

        // write content to file.
        if (ReadFile(handle,
                     buf, 100, &rbytes, NULL)) {
            printf("read succ, rbytes = %d\n", rbytes);
            if (rbytes > 0) {
                buf[rbytes] = 0;
                printf("%s\n", buf);
            }
        }
        else {
            fprintf(stderr, "read file failed %d\n", GetLastError());
            return 1;
        }
    }

    // seek + read testing
    if (0) {
        handle = CreateFile("abc.txt",
                            GENERIC_READ | GENERIC_WRITE,
                            FILE_SHARE_READ | FILE_SHARE_WRITE,
                            NULL,
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);

        if (handle == INVALID_HANDLE_VALUE) {
            fprintf(stderr, "open file failed: %d\n", GetLastError());
            return 1;
        }

        // seek to 2
        DWORD r = SetFilePointer(handle, 2, NULL, FILE_BEGIN);

        if (r != 2) {
            fprintf(stderr, "seek failed: %d, last error\n", r,
                    GetLastError());
            return 1;
        }

        char buf[100];
        DWORD rbytes;

        // write content to file.
        if (ReadFile(handle,
                     buf, 100, &rbytes, NULL)) {
            printf("read succ, rbytes = %d\n", rbytes);
            if (rbytes > 0) {
                buf[rbytes] = 0;
                printf("%s\n", buf);
            }
        }
        else {
            fprintf(stderr, "read file failed %d\n", GetLastError());
            return 1;
        }
    }

    // seek + write testing
    if (1) {
        handle = CreateFile("abc.txt",
                            GENERIC_READ | GENERIC_WRITE,
                            FILE_SHARE_READ | FILE_SHARE_WRITE,
                            NULL,
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);

        if (handle == INVALID_HANDLE_VALUE) {
            fprintf(stderr, "create file failed: %d\n", GetLastError());
            return 1;
        }

        // seek to 2
        DWORD r = SetFilePointer(handle, 2, NULL, FILE_BEGIN);

        if (r != 2) {
            fprintf(stderr, "seek failed: %d, last error\n", r,
                    GetLastError());
            return 1;
        }

        const char *str = "XX";
        DWORD wbytes;

        // write content to file.
        if (WriteFile(handle,
                      str, 2, &wbytes, NULL)) {
            if (wbytes != 2) {
                fprintf(stderr, "write failed: %d\n", GetLastError());
                return 1;
            }

            printf("write ok\n");
        }
        else {
            fprintf(stderr, "write file failed %d\n", GetLastError());
            return 1;
        }
    }

    return 0;
}
